from django.db import models
from django.db.models import PROTECT,DO_NOTHING
# Create your models here.
from django.db.models import CASCADE
from ckeditor_uploader.fields import RichTextUploadingField

class Type(models.Model):
    id = models.AutoField(primary_key=True,help_text='type的主键')
    # 文章站类型表格
    choice_statues = ((1, '显示'), (0, '隐藏'))
    type_statues = models.IntegerField(default=0, blank=False, choices=choice_statues)
    typename = models.CharField(max_length=20,default='type1', blank=False,null=False)
    typeIMG = models.ImageField(upload_to='scoreimgs/%Y/%m/%d', default=None, blank=True, null=True)
    typeweight = models.IntegerField(default=1, blank=False, null=False)  # 禁止留空(类型权重)
    def __str__(self):
        return f'{self.typename}类型，权重为{self.typeweight}'
    class Meta:
        verbose_name = '抖音文章分享站-文章站分类表'
        verbose_name_plural = verbose_name

#默认外键的设置
def get_Type_FK_id():
    return Type.objects.get_or_create(id=1)[0].id

#默认外键的设置
def get_PopUp_FK_id():
    return BookPopInfo.objects.get_or_create(ID=1)[0].ID
class BookPageContent(models.Model):
    Img = models.ImageField(upload_to='scoreimgs/%Y/%m/%d', default=None, blank=True, null=True)
    ID = models.AutoField(primary_key=True)
    choice_statues = ((1, '显示'), (0, '隐藏'))
    ifShow = models.IntegerField(verbose_name='帖子状态', default=0, blank=False, choices=choice_statues)  # 禁止留空
    choiceOnIndex = ((1, '显示'), (0, '不显示'))
    ifShowIndex = models.IntegerField(verbose_name='首页推荐', default=0, blank=False, choices=choiceOnIndex)
    score_type = models.ForeignKey('Type', related_name='scoretype', default=get_Type_FK_id, blank=False,
                                   on_delete=CASCADE,help_text='介绍：对应的外键是Type类')  # blank =false 表单必须填写内容
    TypeTitle = models.CharField(verbose_name='封面分类标题',max_length=50, blank=True,default='',null=True)
    BookName = models.CharField(verbose_name='书名',max_length=50, blank=True,default='',null=True)
    IntroWord = models.CharField(verbose_name='封面介绍语句',max_length=50, blank=True,default='',null=True)
    author = models.CharField(verbose_name='作者',max_length=50, blank=True,default='',null=True)
    master = models.CharField(verbose_name='主角',max_length=50, blank=True,default='',null=True)
    Title = models.CharField(verbose_name='小说标题',max_length=50, blank=True,default='',null=True,help_text='介绍：文章页面的标题')
    # 设置富文本ckeditor,保存图文信息
    Content = RichTextUploadingField(verbose_name='小说内容，HTML格式',blank=True,default='',null=True)
    clickNum = models.IntegerField(verbose_name='查看人数', default=0, blank=False, null=False)
    UPDATED_TIME = models.DateTimeField(auto_now_add=True)




    # 给每个单页都添加一个弹出外键，动态修改不同的内容
    BookPageContent_PopInfo = models.ForeignKey('BookPopInfo',related_name='bookPageContentPopInfo',default=get_PopUp_FK_id,blank=True,null=True,
                                                on_delete=CASCADE,help_text='介绍：对应的弹出框内容')

    def __str__(self):
        # return f'id:[{self.fenxiangid}],{self.score_type.typename}类型的账号：{self.fenxiangname}，权重为：{self.itemweight}'

        return f"{self.ID}.{self.Title}    {self.clickNum}"

    class Meta:
        verbose_name = '抖音文章分享站-论坛帖子内容页'
        verbose_name_plural = verbose_name

#弹出
class BookPopInfo(models.Model):
    choice_statues = ((1, '显示'), (0, '隐藏'))
    ID = models.AutoField(primary_key=True)
    IfShow = models.IntegerField(default=0, blank=False, choices=choice_statues)
    ImageSRC = models.CharField(verbose_name='二维码链接',max_length=300, blank=True,default='',null=True)
    UPDATED_TIME = models.DateTimeField(auto_now_add=True)
    GZHName = models.CharField(verbose_name='小说精选', max_length=50, blank=True, default='', null=True)  #公众号名字

    def __str__(self):
        return f"二维码地址：{self.ImageSRC}"

    class Meta:
        verbose_name = '抖音文章分享站-小程序二维码配置'
        verbose_name_plural = verbose_name

